package controllers;

import models.Jpword;
import models.Record;
import org.joda.time.DateTime;
import play.cache.Cache;
import play.mvc.Controller;

import java.util.List;

public class Application extends Controller {
    private static int fint = 10;

    public static void index() {
      DateTime stTimeDt = new DateTime();
      String stTime = stTimeDt.toString("yyyy-MM-dd HH:mm:ss.SSS");
      List<Jpword> jquizs = Jpword.find("txt = ? order by rand()", "05").fetch(fint);
      Cache.set("jquizs", jquizs);
      render(jquizs, stTime);
    }

    public static void confirm() {
      String jqHr[] = params.getAll("jqHr");
      String jqr[] = params.getAll("jqr");
      String jqId[] = params.getAll("jqId");
      DateTime dateTime = new DateTime();

      int score = 0;
      String memo = "";

      for(int i=0;i<fint-1;i++) {
          if(jqHr[i].equals(jqr[i])) {
              score++;
          } else {
        	   memo += jqId[i] + ",";
          }
      }

      List<Jpword> jquizs = Cache.get("jquizs", List.class);
      Record rd = new Record("51recite", "0", "007");
      rd.cls = jquizs.get(1).cls;
      rd.text = jquizs.get(1).txt;
      rd.type = "51recite";
      rd.score = String.valueOf(score);
      rd.usrId = "007";
      rd.rhost = "127.0.0.1";
      rd.stTime =  params.get("stTime");
      rd.addDate = dateTime.toString("yyyy-MM-dd HH:mm:ss.SSS");
      rd.memo= memo;
      rd.save();

      //List<Jpword> jquizs = Jpword.find("order by rand()").fetch(fint);
      render(jquizs);
    }
}